package com.service.notice;


import com.common.PojoUtil;
import com.common.ResultUtil;
import com.entity.Notice;
import com.entity.OperationLog;
import com.facade.log.IOperationLogFacadeCUD;
import com.facade.notice.INoticeFacadeCUD;
import com.facade.notice.INoticeFacadeR;
import com.mapper.CustomMapper;
import com.mapper.NoticeMapper;
import com.service.CustomServiceCUD;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;

@Service
public class NoticeServiceCUD extends CustomServiceCUD<Notice> implements INoticeFacadeCUD {

    @Resource
    private NoticeMapper mapper;
    @Resource
    private INoticeFacadeR noticeFacadeR;
    @Resource
    private IOperationLogFacadeCUD operationLogFacadeCUD;

    @Override
    protected CustomMapper<Notice> initMapper() {
        return mapper;
    }

    @Override
    public ResultUtil addNotice(Notice notice, Serializable... managerId) {
        ResultUtil resultUtil = ResultUtil.newInstance();
        Notice _$0 = noticeFacadeR.findUniqueByPropertyEqualTo("title", notice.getTitle());
        if (null != _$0) {
            resultUtil.setStatus(ResultUtil.PARAM_VALID_FAILED);
            resultUtil.setMessage("标题已存在");
            return resultUtil;
        }
        addSelective(notice);
        if (null != managerId && managerId.length > 0) {
            String message = "添加公告：[" + PojoUtil.notNull(notice, "", "id") + "]";
            operationLogFacadeCUD.addOperationLog(managerId[0], OperationLog.ADD, message);
        }
        resultUtil.setMessage("添加成功");
        return resultUtil;
    }

    @Override
    public ResultUtil modifyNotice(Notice notice, Serializable... managerId) {
        ResultUtil resultUtil = ResultUtil.newInstance();
        Long id = notice.getId();
        if (null != id) {
            Notice _$0 = noticeFacadeR.findByPrimaryKey(id);
            if (null == _$0) {
                resultUtil.setStatus(ResultUtil.FAILED);
                resultUtil.setMessage("记录不存在或已被删除");
                return resultUtil;
            }
            Notice _$1 = noticeFacadeR.findUniqueByPropertyEqualTo("title", notice.getTitle());
            if (null != _$1 && !_$1.getId().equals(id)) {
                resultUtil.setStatus(ResultUtil.PARAM_VALID_FAILED);
                resultUtil.setMessage("标题已存在");
                return resultUtil;
            }
            update(notice);
            if (null != managerId && managerId.length > 0) {
                String message = "修改公告：[" + PojoUtil.compareDifferent(_$0, notice, "", "id", "title") + "]";
                operationLogFacadeCUD.addOperationLog(managerId[0], OperationLog.MODIFY, message);
            }
            resultUtil.setMessage("修改成功");
        }
        return resultUtil;
    }

    @Override
    public ResultUtil deleteNotice(Serializable id, Serializable... managerId) {
        ResultUtil resultUtil = ResultUtil.newInstance();
        Notice notice = noticeFacadeR.findByPrimaryKey(id);
        if (null == notice) {
            resultUtil.setStatus(ResultUtil.FAILED);
            resultUtil.setMessage("记录不存在或已被删除");
            return resultUtil;
        }
        deleteByPrimaryKey(id);
        if (null != managerId && managerId.length > 0) {
            String message = "删除公告：[" + PojoUtil.notNull(notice, "", "id", "title") + "]";
            operationLogFacadeCUD.addOperationLog(managerId[0], OperationLog.DELETE, message);
        }
        resultUtil.setMessage("删除成功");
        return resultUtil;
    }
}